package com.qkl.xiatang.app.utils.appenum;

import com.qkl.xiatang.app.dto.user.UserDutiesDTO;
import com.qkl.xiatang.app.utils.AppUtils;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * app用户身份、职务
 * 身份，0管理，3查料，2配料，1喂料，4称重，5 客户
 */
@Getter
public enum AppUserDutiesEnum {

    FEED("1","喂料"),
    ALLOC("2","配料"),
    CHECK("3","查料"),
    WEIGH("4","称料"),
    HOLDER("5","客户"),
    OBSERVE("6","督察员"),
    ADMIN("0","管理"),
    ;

    private AppUserDutiesEnum(String duties,String name){
        this.duties = duties;
        this.name = name;
        EnumUtil.MAP_ENUM.put(duties,this);
    }

    private String duties;

    private String name;

    private UserDutiesDTO toDuties(){
        UserDutiesDTO dto = new UserDutiesDTO();
        dto.setName(name);
        dto.setType(duties);
        return dto;
    }


    public static boolean isFeed(String duties){
        return isDuties(FEED,duties);
    }

    public static boolean isAlloc(String duties){
        return isDuties(ALLOC,duties);
    }

    public static boolean isCheck(String duties){
        return isDuties(CHECK,duties);
    }

    public static boolean isAdmin(String duties){
        return isDuties(ADMIN,duties);
    }

    public static boolean isWeigh(String duties){
        return isDuties(WEIGH,duties);
    }

    public static boolean isHolder(String duties){
        return isDuties(HOLDER,duties);
    }

    public static boolean isObserve(String duties){
        return isDuties(OBSERVE,duties);
    }

    public static boolean isDuties(String duties){
        return isFeed(duties) || isAlloc(duties) || isCheck(duties) || isAdmin(duties) || isWeigh(duties);
    }

    public static List<UserDutiesDTO> dutiesList(){
        List<UserDutiesDTO> list = new ArrayList<>();
        Collection<AppUserDutiesEnum>  collection = EnumUtil.MAP_ENUM.values();
        for(AppUserDutiesEnum dutiesEnum : collection){
            list.add(dutiesEnum.toDuties());
        }
        return list;
    }

    public static boolean isDuties(String type,String duties){
        if(StringUtils.isAnyEmpty(type,duties)){
            return false;
        }
        List<String> dutiesList = AppUtils.stringToList(duties);
        return dutiesList.contains(type);
    }

    private static boolean isDuties(AppUserDutiesEnum dutiesEnum,String duties){
        return isDuties(dutiesEnum.getDuties(),duties);
    }

    private static  class EnumUtil{
        public static Map<String,AppUserDutiesEnum> MAP_ENUM = new HashMap<>();
    }

}
